<?php

/*
// +--------------------------------------------------------------------------+
// | Project:    TBDevYSE - TBDev Yuna Scatari Edition                        |
// +--------------------------------------------------------------------------+
// | This file is part of TBDevYSE. TBDevYSE is based on TBDev,               |
// | originally by RedBeard of TorrentBits, extensively modified by           |
// | Gartenzwerg.                                                             |
// |                                                                          |
// | TBDevYSE is free software; you can redistribute it and/or modify         |
// | it under the terms of the GNU General Public License as published by     |
// | the Free Software Foundation; either version 2 of the License, or        |
// | (at your option) any later version.                                      |
// |                                                                          |
// | TBDevYSE is distributed in the hope that it will be useful,              |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            |
// | GNU General Public License for more details.                             |
// |                                                                          |
// | You should have received a copy of the GNU General Public License        |
// | along with TBDevYSE; if not, write to the Free Software Foundation,      |
// | Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            |
// +--------------------------------------------------------------------------+
// |                                               Do not remove above lines! |
// +--------------------------------------------------------------------------+
*/

# IMPORTANT: Do not edit below unless you know what you are doing!
if(!defined('IN_ANNOUNCE'))
  die('Hacking attempt!');
function err($msg) {
	benc_resp(array("failure reason" => array(type => "string", value => $msg)));
	exit();
}

function benc_resp($d) {
	benc_resp_raw(benc(array(type => "dictionary", value => $d)));
}

function benc_resp_raw($x) {
	header("Content-Type: text/plain");
	header("Pragma: no-cache");
	print($x);
}

function get_date_time($timestamp = 0) {
	if ($timestamp)
		return date("Y-m-d H:i:s", $timestamp);
	else
		return date("Y-m-d H:i:s");
}

function gmtime() {
    return strtotime(get_date_time());
}

function strip_magic_quotes($arr) {
	foreach ($arr as $k => $v)
	{
	 if (is_array($v))
	  { $arr[$k] = strip_magic_quotes($v); }
	 else
	  { $arr[$k] = stripslashes($v); }
	}

	return $arr;
}

function mksize($bytes) {
	if ($bytes < 1000 * 1024)
		return number_format($bytes / 1024, 2) . " kB";
	elseif ($bytes < 1000 * 1048576)
		return number_format($bytes / 1048576, 2) . " MB";
	elseif ($bytes < 1000 * 1073741824)
		return number_format($bytes / 1073741824, 2) . " GB";
	else
		return number_format($bytes / 1099511627776, 2) . " TB";
}

function emu_getallheaders() {
   foreach($_SERVER as $name => $value)
	   if(substr($name, 0, 5) == 'HTTP_')
		   $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
   return $headers;
}

function portblacklisted($port) {
	if ($port >= 411 && $port <= 413)
		return true;
	if ($port >= 6881 && $port <= 6889)
		return true;
	if ($port == 1214)
		return true;
	if ($port >= 6346 && $port <= 6347)
		return true;
	if ($port == 4662)
		return true;
	if ($port == 6699)
		return true;
	return false;
}

function validip($ip) {
	if (!empty($ip) && $ip == long2ip(ip2long($ip)))
	{
				$reserved_ips = array (
				array('0.0.0.0','2.255.255.255'),
				array('10.0.0.0','10.255.255.255'),
				array('127.0.0.0','127.255.255.255'),
				array('169.254.0.0','169.254.255.255'),
				array('172.16.0.0','172.31.255.255'),
				array('192.0.2.0','192.0.2.255'),
				array('192.168.0.0','192.168.255.255'),
				array('255.255.255.0','255.255.255.255')
		);

		foreach ($reserved_ips as $r)
		{
				$min = ip2long($r[0]);
				$max = ip2long($r[1]);
				if ((ip2long($ip) >= $min) && (ip2long($ip) <= $max)) return false;
		}
		return true;
	}
	else return false;
}

function getip() {
   if (isset($_SERVER)) {
     if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && validip($_SERVER['HTTP_X_FORWARDED_FOR'])) {
       $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
     } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && validip($_SERVER['HTTP_CLIENT_IP'])) {
       $ip = $_SERVER['HTTP_CLIENT_IP'];
     } else {
       $ip = $_SERVER['REMOTE_ADDR'];
     }
   } else {
     if (getenv('HTTP_X_FORWARDED_FOR') && validip(getenv('HTTP_X_FORWARDED_FOR'))) {
       $ip = getenv('HTTP_X_FORWARDED_FOR');
     } elseif (getenv('HTTP_CLIENT_IP') && validip(getenv('HTTP_CLIENT_IP'))) {
       $ip = getenv('HTTP_CLIENT_IP');
     } else {
       $ip = getenv('REMOTE_ADDR');
     }
   }

   return $ip;
 }

function hash_pad($hash) {
    return str_pad($hash, 20);
}

function hash_where($name, $hash) {
    $shhash = preg_replace('/ *$/s', "", $hash);
    return "($name = " . sqlesc($hash) . " OR $name = " . sqlesc($shhash) . ")";
}

function unesc($x) {
	if (get_magic_quotes_gpc()) return stripslashes($x);
	return $x;
}

function gzip() {
	if (@extension_loaded('zlib') && @ini_get('zlib.output_compression') != '1' && @ini_get('output_handler') != 'ob_gzhandler') {
		@ob_start('ob_gzhandler');
	}
}

function BannedClients() {
    if(substr($peer_id, 0, 6) == 'exbc\08') err('BitComet 0.56 is Banned, Upgrade.');
    if(substr($peer_id, 0, 4) == 'FUTB') err('FUTB? Fuck You Too.'); //patched version of BitComet 0.57 (FUTB- Fuck U TorrentBits)
    if(substr($peer_id, 1, 2) == 'BC' && substr($peer_id, 5, 2) != 70 && substr($peer_id, 5, 2) != 63 && substr($peer_id, 5, 2) != 77 && substr($peer_id, 5, 2) >= 59/* && substr($peer_id, 5, 2) <= 88*/) err("BitComet ".substr($peer_id, 5, 2)." is banned. Use only 0.70 or switch to uTorrent 1.6.1.");
    if(substr($peer_id, 1, 2) == 'UT' && substr($peer_id, 3, 3) >= 170 && substr($peer_id, 3, 3) <= 174) err('uTorrent '.substr($peer_id, 3, 3).' is banned. Downgrade to 1.6.1 or use 1.7.5 or higher.');
    if(ereg("^ABC\\/ABC", $agent)) err('ABC is Banned.');
    if(ereg("^0P3R4H", $agent)) err('IBrowser Opera is not a cool BT client.');
    if(substr($peer_id, 0, 4) == 'FUTB') err('FUTB? Fuck You Too.');
    if(substr($peer_id, 0, 7) == 'exbc\0L') err('BitLord 1.0 is Banned.');
    if(substr($peer_id, 0, 7) == 'exbcL') err('BitLord 1.1 is Banned.');
    if(substr($peer_id, 0, 3) == '-TS') err('TorrentStorm is Banned.');
    if(substr($peer_id, 0, 5) == "Mbrst") err("Burst! is Banned.");
    if(substr($peer_id, 0, 3) == "-BB") err("BitBuddy is Banned.");
    if(substr($peer_id, 0, 3) == "-SZ") err("Shareaza is Banned.");
    if(substr($peer_id, 0, 5) == "turbo") err("TurboBT is banned.");
    if(substr($peer_id, 0, 4) == "T03A") err("Please Update your BitTornado.");
    if(substr($peer_id, 0, 4) == "T03B") err("Please Update your BitTornado.");
    if(substr($peer_id, 0, 3 ) == "FRS") err("Rufus is Banned.");
    if(substr($peer_id, 0, 2 ) == "eX") err("eXeem is Banned.");
    if(substr($peer_id, 0, 8 ) == "-TR0005-") err("Transmission/0.5 is Banned.");
    if(substr($peer_id, 0, 8 ) == "-TR0006-") err("Transmission/0.6 is Banned.");
    if(substr($peer_id, 0, 8 ) == "-XX0025-") err("Transmission/0.6 is Banned.");
    if(substr($peer_id, 0, 1 ) == ",") err ("RAZA is banned.");
    if(substr($peer_id, 0, 3 ) == "-AG") err("This is a banned client. We recommend uTorrent or Azureus.");
    if(substr($peer_id, 0, 3 ) == "R34") err("BTuga/Revolution-3.4 is not an acceptalbe client. Please read the FAQ on recommended clients.");
    if(preg_match("/MLDonkey\/([0-9]+).([0-9]+).([0-9]+)*/", $agent, $matches)) err("MLDonkey is not a BT client.");
    if(preg_match("/ed2k_plugin v([0-9]+\\.[0-9]+).*/", $agent, $matches)) err("eDonkey is not a BT client.");
    if(substr($peer_id, 0, 4) == "exbc") err("This version of BitComet is banned! You can thank DHT for this ban!");
    if (substr($peer_id, 0, 3) == '-FG') err("FlashGet is banned!");
    return ;
}


function deadtime() {
	global $options;
	return time() - floor($options['announce_interval'] * 1.3);
}

?>
